import 'dart:math';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:fish_redux/fish_redux.dart';
import '../../app_files.dart';
import 'action.dart';
import 'state.dart';

Effect<UserLoginState> buildEffect() {
  return combineEffects(<Object, Effect<UserLoginState>>{
    Lifecycle.initState: _initState,
    UserLoginAction.onSubmit: _onSubmit,
    UserLoginAction.onTapRegister: _onTapRegister,
  });
}

void _initState(Action action, Context<UserLoginState> ctx) async {
  // CommonNetWork.getConfigOptions();

//  String phone = await CommonUtils.getPubDefaultValue('editPhone');
//  if (phone != null) ctx.state.phoneController.text = phone;
//  String password = await CommonUtils.getPubDefaultValue('editPassword');
//  if (password != null) ctx.state.passwordController.text = password;
//  ctx.dispatch(UserLoginActionCreator.onAction());
}

void _onSubmit(Action action, Context<UserLoginState> ctx) async {
  if (ctx.state.phoneController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入您的手机号');
    return;
  }
  if (ctx.state.passwordController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入您的密码');
    return;
  }

  var number = double.tryParse(ctx.state.phoneController.text);

  ///展示用户界面
  if (ctx.state.phoneController.text.length == 11 && number != null) {
    CommonUtils.showWaitLoading(ctx.context);
    ResultData res = await CommonNetWork.gasUserLogin(
      ctx.state.phoneController.text,
      ctx.state.passwordController.text,
    );
    Navigator.pop(ctx.context);
    if (res.success) {
      CommonUtils.setPubDefaultValue(
        phone: ctx.state.phoneController.text,
        password: ctx.state.passwordController.text,
      );
      UserInfoEntity info = UserInfoEntity.fromGasJson(res.data);
      await CommonUtils.initImageConfig(token: info.token,isGasUser: true);
      ResultData infoRes = await CommonNetWork.gasUserCenterInfo(
          token: info.token, memberID: info.userid);
      if (infoRes.success) {
        info.setGasInfoFromJson(infoRes.data);
        info.password = ctx.state.passwordController.text;
        info.mobile = ctx.state.phoneController.text;
        info.password = ctx.state.passwordController.text;
        info.isGsUser = true;
        GlobalStore.store.dispatch(GlobalActionCreator.onUserLogin(info));
        bus.emit('setGasUser');
        Navigator.popUntil(ctx.context, ModalRoute.withName('/'));
      } else {
        CommonUtils.showErrorInfo(ctx.context, res.desc);
      }
    } else {
      CommonUtils.showErrorInfo(ctx.context, res.desc);
    }
  }

  ///展示员工界面
  else {
    CommonUtils.showErrorInfo(ctx.context, '请输入燃气用户手机号');
    // CommonUtils.showWaitLoading(ctx.context);
    // ResultData res = await CommonNetWork.userLogin(
    //     userName: ctx.state.phoneController.text,
    //     password: ctx.state.passwordController.text);
    // Navigator.pop(ctx.context);
    // if (res.success) {
    //   CommonUtils.setPubDefaultValue(
    //     phone: ctx.state.phoneController.text,
    //     password: ctx.state.passwordController.text,
    //   );
    //   UserInfoEntity info = UserInfoEntity.fromJson(res.data);
    //   ResultData infoRes = await CommonNetWork.userCenterInfo(
    //       token: info.token, memberID: info.userid);
    //   if (infoRes.success) {
    //     info.setInfoFromJson(infoRes.data);
    //     info.password = ctx.state.passwordController.text;
    //     info.mobile = ctx.state.phoneController.text;

    //     if (info.roleNames.length == 0) {
    //       CommonUtils.showErrorInfo(ctx.context, '该账号没有app支持角色');
    //       return;
    //     }
    //     if (info.roleNames.length == 1) {
    //       info.currentRoleName = info.roleNames.first;
    //     }
    //     else {
    //       String pos = preferences.getString(UserInfoConst.position + info.userid) ?? '';
    //       info.currentRoleName = pos;
    //     }
    //     // if (info.plans != null && info.plans.isEmpty && info.isCheckWorker) {
    //     //   CommonUtils.showErrorInfo(ctx.context, '未有安检任务,切换至维修员角色');
    //     //   info.currentRoleName = '维修员';
    //     // }
    //     print('${ info.currentRoleName  }');
    //     GlobalStore.store.dispatch(GlobalActionCreator.onUserLogin(info));
    //
    //     if (info.currentRoleName.length == 0) {
    //       CommonUtils.presentReplaceRoute(ctx.context, RoutePath.userTypeView);
    //     } else {
    //       Navigator.pop(ctx.context);
    //       Future.delayed(Duration(milliseconds: 400)).then((value) {
    //         bus.emit('refreshMain');
    //       });
    //     }
    //   } else {
    //     CommonUtils.showErrorInfo(ctx.context, infoRes.desc);
    //   }
    // } else {
    //   CommonUtils.showErrorInfo(ctx.context, res.desc);
    // }
  }
}

void _onTapRegister(Action action, Context<UserLoginState> ctx) async {
  var res =
      await CommonUtils.pushRoute(ctx.context, RoutePath.userRegisterView);
  if (res != null) {
    ctx.state.phoneController.text = res['mobile'];
    ctx.state.passwordController.text = res['password'];
    ctx.dispatch(UserLoginActionCreator.onAction());
  }
}
